package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.http.OnSwitchListener;
import com.dinadi.yidi.ui.MyApplication;
import com.dinadi.yidi.ui.view.CommonDialogUtils;
import com.dinadi.yidi.ui.view.SwitchButton;
import com.dinadi.yidi.utils.SharePreferencesHelper;

/**
 * author: JiaYu.He
 * Date: 2017/3/22     设置页面
 * Time: 16:29
 */

public class UserSettingsActivity extends BaseActivity {

    private View view;
    private TextView tvTitle;
    private SwitchButton switchButton;
    private RelativeLayout edit_profile, about_me, clear_buffer,address_management;
    private ImageView btn_exit_login;
    private CommonDialogUtils myDialog;
    private boolean loginState;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(UserSettingsActivity.this).inflate(R.layout.activity_user_settings, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_settings));
        edit_profile = (RelativeLayout) view.findViewById(R.id.edit_profile);
        switchButton = (SwitchButton) view.findViewById(R.id.switch_button);
        about_me = (RelativeLayout) view.findViewById(R.id.about_me);
        btn_exit_login = (ImageView) view.findViewById(R.id.btn_exit_login);
        clear_buffer = (RelativeLayout) view.findViewById(R.id.clear_buffer);
        address_management = (RelativeLayout) view.findViewById(R.id.address_management);
        switchButton.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange() {
                if (switchButton.getCurrentStatus() == SwitchButton.CLOSE) {
                    Toast.makeText(UserSettingsActivity.this, "关闭推送", Toast.LENGTH_SHORT).show();
                } else if (switchButton.getCurrentStatus() == SwitchButton.OPEN) {
                    Toast.makeText(UserSettingsActivity.this, "开启推送", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void initListener() {
        registerClickListener(edit_profile, about_me, btn_exit_login, clear_buffer,address_management);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {

            case R.id.edit_profile:
                //编辑资料
                startActivity(new Intent(UserSettingsActivity.this, EditProfileActivity.class));
                break;
            case R.id.about_me:
                /*关于我们*/
                startActivity(new Intent(UserSettingsActivity.this, AboutMeActivity.class));
                break;
            case R.id.btn_exit_login:
                /*退出登录*/
                exitLogin();
                break;
            case R.id.clear_buffer:
                toast("清除成功");
                break;
            case R.id.address_management:
                /*地址管理*/
                Intent intent=new Intent(UserSettingsActivity.this,AddressManagementActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void exitLogin() {
        myDialog = new CommonDialogUtils(UserSettingsActivity.this);
        myDialog.setContent(R.string.exit_login);
        myDialog.setDialogCallback(dialogcallback);
        myDialog.show();
    }

    /**
     * 设置mydialog需要处理的事情
     */
    CommonDialogUtils.Dialogcallback dialogcallback = new CommonDialogUtils.Dialogcallback() {
        @Override
        public void dialogdo() {
            //token值
            SharePreferencesHelper.getInstance(UserSettingsActivity.this).putString(
                    SharePreferencesHelper.SP_TOKEN, "");
            //用户id/uid
            SharePreferencesHelper.getInstance(UserSettingsActivity.this).putString(
                    SharePreferencesHelper.SP_UID, "");
            MyApplication.getApp().getUser().setHeadImg("");
            MyApplication.getApp().getUser().setNickName("");
            MyApplication.getApp().getUser().setBrandName("");
            MyApplication.getApp().getUser().setSignature("");
            toast("退出登录成功");
            UserSettingsActivity.this.finish();
            loginState = false;
            SharePreferencesHelper.getInstance(UserSettingsActivity.this).putBoolean("loginState", loginState);

            myDialog.dismiss();
//            AppManager.getAppManager().finishAllActivity();
//            //跳转登录页面
//            Intent intent = new Intent(UserSettingsActivity.this, LoginActivity.class);
//            startActivity(intent);

        }
    };

}
